# 第一步：导入FastAPI
from typing import List

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from starlette.middleware.cors import CORSMiddleware
# 第二步：创建FastAPI应用
app = FastAPI()

# 配置CORS中间件，允许前端跨域请求
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173", "http://localhost:5174", "http://localhost:3000"],  # 允许的前端地址
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
# 数据模型定义
class FeedItem(BaseModel):
    id: int
    type: str = "image"
    title: str
    author: str
    avatar: str
    likes: int
    image: str
    tags: List[str] = []

    @app.get("/")
    async def root(self):
        """根路径，返回API信息"""
        return {
            "message": "HomeFeeds API",
            "version": "1.0.0",
            "status": "running"
        }

    # 模拟数据 - 增加更多数据
    mock_data = [
        {
            "id": 1,
            "type": "image",
            "title": "大学生活指南：如何平衡学习与社交",
            "author": "学霸小王",
            "avatar": "https://d.musicapp.migu.cn/prod/playlist-service/playListimg/402bdb81-c298-4582-b208-543920fb8b08.jpg",
            "likes": 1288,
            "image": "https://n.sinaimg.cn/ent/transform/20160326/z7HN-fxqswxx0255082.jpg",
            "tags": ["学习", "生活"]
        },
        {
            "id": 2,
            "type": "image",
            "title": "宿舍美食分享：简单易做的夜宵",
            "author": "美食达人",
            "avatar": "https://d.musicapp.migu.cn/prod/playlist-service/playListimg/402bdb81-c298-4582-b208-543920fb8b08.jpg",
            "likes": 856,
            "image": "https://img.shetu66.com/2023/06/12/1686543631054698.jpg",
            "tags": ["美食", "宿舍"]
        },
        {
            "id": 3,
            "type": "image",
            "title": "周一OOTD",
            "author": "杰伦",
            "avatar": "https://d.musicapp.migu.cn/prod/playlist-service/playListimg/402bdb81-c298-4582-b208-543920fb8b08.jpg",
            "likes": 1288,
            "image": "https://ts1.tc.mm.bing.net/th/id/OIP-C.1E_YP-GilA6QItz-9A4b6gHaKx?rs=1&pid=ImgDetMain&o=7&rm=3",
            "tags": ["穿搭", "时尚"]
        },
        {
            "id": 4,
            "type": "image",
            "title": "校园穿搭：秋日校园风搭配",
            "author": "时尚博主",
            "avatar": "https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=40&h=40&fit=crop&crop=face",
            "likes": 1567,
            "image": "https://n.sinaimg.cn/sinakd20210719ac/345/w1080h1665/20210719/dbca-7817a6becee1b4e492eefd6997e4bd32.jpg",
            "tags": ["穿搭", "校园"]
        },
        {
            "id": 5,
            "type": "image",
            "title": "图书馆学习日常",
            "author": "书虫小李",
            "avatar": "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=40&h=40&fit=crop&crop=face",
            "likes": 743,
            "image": "https://p3-pc-sign.douyinpic.com/tos-cn-i-0813c000-ce/osM7iAagBVApBU88DanTPAiIARrP9b6EAyAyh~tplv-dy-aweme-images:q75.webp?biz_tag=aweme_images&from=327834062&lk3s=138a59ce&s=PackSourceEnum_SEO&sc=image&se=false&x-expires=1762754400&x-signature=zo8UsVscObPqjTFJPb8Yoo0Ve08%3D",
            "tags": ["学习", "图书馆"]
        },
        {
            "id": 6,
            "type": "image",
            "title": "大学生兼职经验分享",
            "author": "兼职达人",
            "avatar": "https://images.unsplash.com/photo-1500648767791-00dcc994a43e?w=40&h=40&fit=crop&crop=face",
            "likes": 1892,
            "image": "https://n.sinaimg.cn/sinakd20210719ac/354/w1080h1674/20210719/c68b-51830ac8f563c761c06eb82ef28bb03e.jpg",
            "tags": ["兼职", "经验"]
        },
        {
            "id": 7,
            "type": "image",
            "title": "校园运动：晨跑打卡分享",
            "author": "运动达人",
            "avatar": "https://images.unsplash.com/photo-1506794778202-cad84cf45f1d?w=40&h=40&fit=crop&crop=face",
            "likes": 1123,
            "image": "https://img2.baidu.com/it/u=2827827884,3870677655&fm=253&app=138&f=JPEG?w=800&h=1319",
            "tags": ["运动", "校园"]
        },
        {
            "id": 8,
            "type": "image",
            "title": "宿舍装饰：打造温馨小窝",
            "author": "生活家",
            "avatar": "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=40&h=40&fit=crop&crop=face",
            "likes": 987,
            "image": "https://img1.baidu.com/it/u=3140992392,596372088&fm=253&app=138&f=JPEG?w=500&h=889",
            "tags": ["生活", "宿舍"]
        },
        {
            "id": 9,
            "type": "image",
            "title": "校园旅行：周末短途游推荐",
            "author": "旅行者",
            "avatar": "https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=40&h=40&fit=crop&crop=face",
            "likes": 1456,
            "image": "https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=400&h=600&fit=crop",
            "tags": ["旅行", "校园"]
        },
        {
            "id": 10,
            "type": "image",
            "title": "大学生创业经验分享",
            "author": "创业者",
            "avatar": "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=40&h=40&fit=crop&crop=face",
            "likes": 2134,
            "image": "https://images.unsplash.com/photo-1559136555-9303baea8ebd?w=400&h=550&fit=crop",
            "tags": ["创业", "经验"]
        },
        {
            "id": 11,
            "type": "image",
            "title": "校园美食探店：食堂新菜品",
            "author": "美食探索者",
            "avatar": "https://ts1.tc.mm.bing.net/th/id/R-C.4245d003ab5808d364ad42c9162f73b4?rik=jD5IWnobXSWE1g&riu=http%3a%2f%2fpic.baike.soso.com%2fp%2f20090103%2f20090103120000-107987.jpg&ehk=eQfyMMCGaYLRnIP9CR4d1ao%2bhlb0qr7is%2fRBSo7kDoQ%3d&risl=&pid=ImgRaw&r=0",
            "likes": 756,
            "image": "https://ts1.tc.mm.bing.net/th/id/R-C.d8f92328a04cf7d862afcb1094e00874?rik=1xeUCxW3s0aduA&riu=http%3a%2f%2fn.sinaimg.cn%2fsinakd20109%2f422%2fw1024h998%2f20230228%2f7ba7-a33db609e1addbc2df25efb7f47434e1.jpg&ehk=0bMA1cHA%2bVJmKo8IbrPaZc8UxKHRTElEW%2bLvr6%2b4XHQ%3d&risl=&pid=ImgRaw&r=0",
            "tags": ["美食", "校园"]
        },
        {
            "id": 12,
            "type": "image",
            "title": "期末复习攻略：高效学习方法",
            "author": "学霸小张",
            "avatar": "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=40&h=40&fit=crop&crop=face",
            "likes": 1890,
            "image": "https://ts1.tc.mm.bing.net/th/id/R-C.a47a9af02c4ac77c38f38a51eaa39140?rik=5bgzd%2buLOtSW5Q&riu=http%3a%2f%2fn.sinaimg.cn%2fsinakd20113%2f329%2fw1200h1529%2f20211125%2f91d8-55af1481b1a185439d459009a18dfee8.jpg&ehk=CfJjfNhAQXmZCCMQZWZCLzTGOAgbfHvJblh4xjWoYZw%3d&risl=&pid=ImgRaw&r=0",
            "tags": ["学习", "复习"]
        },
        {
            "id": 13,
            "type": "image",
            "title": "校园社团活动：摄影社作品展",
            "author": "摄影爱好者",
            "avatar": "https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=40&h=40&fit=crop&crop=face",
            "likes": 1234,
            "image": "https://logocdn.feigua.cn/BloggLogo/2828841f70091689d85a44a3cf48a21f.jpg-jpg",
            "tags": ["摄影", "社团"]
        },
        {
            "id": 14,
            "type": "image",
            "title": "宿舍生活：室友相处之道",
            "author": "生活达人",
            "avatar": "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=40&h=40&fit=crop&crop=face",
            "likes": 987,
            "image": "https://images.unsplash.com/photo-1586023492125-27b2c045efd7?w=400&h=500&fit=crop",
            "tags": ["生活", "宿舍"]
        },
        {
            "id": 15,
            "type": "image",
            "title": "校园恋爱：如何平衡感情与学业",
            "author": "情感导师",
            "avatar": "https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=40&h=40&fit=crop&crop=face",
            "likes": 2156,
            "image": "https://images.unsplash.com/photo-1516589178581-6cd7833ae3b2?w=400&h=600&fit=crop",
            "tags": ["情感", "校园"]
        }
    ]

    @app.get("/api/home/feeds")
    async def get_home_feeds(
            category: str = "推荐",
            page: int = 1,
            page_size: int = 20
    ):
        """
        获取首页推荐内容

        - **category**: 分类名称，默认为"推荐"
        - **page**: 页码，默认为1
        - **page_size**: 每页数量，默认为20
        """
        try:
            # 根据分类筛选数据（这里简化处理，实际可以根据category参数筛选）
            filtered_data = mock_data

            # 分页处理
            start_index = (page - 1) * page_size
            end_index = start_index + page_size
            page_data = filtered_data[start_index:end_index]

            # 检查是否还有更多数据
            has_more = end_index < len(filtered_data)

            return {
                "success": True,
                "data": page_data,
                "total": len(filtered_data),
                "page": page,
                "pageSize": page_size,
                "hasMore": has_more
            }
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"获取数据失败: {str(e)}")


